package com.dianping.cat.home.resource.transform;

import com.dianping.cat.home.resource.IEntity;
import com.dianping.cat.home.resource.entity.Resource;
import com.dianping.cat.home.resource.entity.ResourceConfig;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;






public class DefaultSaxParser
  extends DefaultHandler
{
/*  28 */   private DefaultLinker m_linker = new DefaultLinker(true);

/*  30 */   private DefaultSaxMaker m_maker = new DefaultSaxMaker();

  private Stack<String> m_tags = new Stack();

/*  34 */   private Stack<Object> m_objs = new Stack();
  
  private IEntity<?> m_entity;
  
/*  38 */   private StringBuilder m_text = new StringBuilder();


/*  41 */   public static ResourceConfig parse(InputStream in) throws SAXException, IOException { return (ResourceConfig)parseEntity(ResourceConfig.class, new InputSource((InputStream)removeBOM(in))); }



/*  45 */   public static ResourceConfig parse(Reader reader) throws SAXException, IOException { return (ResourceConfig)parseEntity(ResourceConfig.class, new InputSource((Reader)removeBOM(reader))); }



/*  49 */   public static ResourceConfig parse(String xml) throws SAXException, IOException { return (ResourceConfig)parseEntity(ResourceConfig.class, new InputSource(new StringReader((String)removeBOM(xml)))); }


  
  private static <T extends IEntity<?>> T parseEntity(Class<T> type, InputSource is) throws SAXException, IOException {
    try {
/*  55 */       DefaultSaxParser handler = new DefaultSaxParser();
/*  56 */       SAXParserFactory factory = SAXParserFactory.newInstance();

/*  58 */       factory.setValidating(false);
/*  59 */       factory.setFeature("http://xml.org/sax/features/validation", false);

/*  61 */       factory.newSAXParser().parse(is, handler);
/*  62 */       return (T)handler.getEntity();
/*  63 */     } catch (ParserConfigurationException e) {
/*  64 */       throw new IllegalStateException("Unable to get SAX parser instance!", e);
    }
  }

  
/*  69 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, InputStream in) throws SAXException, IOException { return (T)parseEntity(type, new InputSource((InputStream)removeBOM(in))); }



/*  73 */   public static <T extends IEntity<?>> T parseEntity(Class<T> type, String xml) throws SAXException, IOException { return (T)parseEntity(type, new InputSource(new StringReader((String)removeBOM(xml)))); }



  
  private static <T> T removeBOM(T obj) throws IOException {
/*  79 */     if (obj instanceof String) {
/*  80 */       String str = (String)obj;

/*  82 */       if (str.length() != 0 && str.charAt(0) == '﻿') {
/*  83 */         return (T)str.substring(1);
      }
/*  85 */       return obj;
    }
/*  87 */     if (obj instanceof InputStream) {
/*  88 */       BufferedInputStream in = new BufferedInputStream((InputStream)obj);

/*  90 */       in.mark(3);

/*  92 */       if (in.read() != 239 || in.read() != 187 || in.read() != 191) {
/*  93 */         in.reset();
      }
      
/*  96 */       return (T)in;
/*  97 */     }  if (obj instanceof Reader) {
/*  98 */       BufferedReader in = new BufferedReader((Reader)obj);

/* 100 */       in.mark(1);

/* 102 */       if (in.read() != 65279) {
/* 103 */         in.reset();
      }
      
/* 106 */       return (T)in;
    }
/* 108 */     return obj;
  }


  
  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 114 */     if (value == null || value.length() == 0) {
/* 115 */       return defaultValue;
    }
    
/* 118 */     if (type == Boolean.class)
/* 119 */       return (T)Boolean.valueOf(value); 
/* 120 */     if (type == Integer.class)
/* 121 */       return (T)Integer.valueOf(value); 
/* 122 */     if (type == Long.class)
/* 123 */       return (T)Long.valueOf(value); 
/* 124 */     if (type == Short.class)
/* 125 */       return (T)Short.valueOf(value); 
/* 126 */     if (type == Float.class)
/* 127 */       return (T)Float.valueOf(value); 
/* 128 */     if (type == Double.class)
/* 129 */       return (T)Double.valueOf(value); 
/* 130 */     if (type == Byte.class)
/* 131 */       return (T)Byte.valueOf(value); 
/* 132 */     if (type == Character.class) {
/* 133 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 135 */     return (T)value;
  }



  
/* 141 */   public void characters(char[] ch, int start, int length) throws SAXException { this.m_text.append(ch, start, length); }




/* 146 */   public void endDocument() { this.m_linker.finish(); }


  
  public void endElement(String uri, String localName, String qName) throws SAXException {
/* 151 */     if (uri == null || uri.length() == 0) {
/* 152 */       this.m_objs.pop();
/* 153 */       this.m_tags.pop();
    }

    
/* 157 */     this.m_text.setLength(0);
  }

  
/* 161 */   private IEntity<?> getEntity() { return this.m_entity; }



/* 165 */   protected String getText() { return this.m_text.toString(); }

  
  private void parseForResource(Resource parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 169 */     this.m_objs.push(parentObj);
/* 170 */     this.m_tags.push(qName);
  }
  
  private void parseForResourceConfig(ResourceConfig parentObj, String parentTag, String qName, Attributes attributes) throws SAXException {
/* 174 */     if ("resource".equals(qName)) {
/* 175 */       Resource resource = this.m_maker.buildResource(attributes);

/* 177 */       this.m_linker.onResource(parentObj, resource);
/* 178 */       this.m_objs.push(resource);
    } else {
/* 180 */       throw new SAXException(String.format("Element(%s) is not expected under resource-config!", new Object[] { qName }));
    }
    
/* 183 */     this.m_tags.push(qName);
  }
  
  private void parseRoot(String qName, Attributes attributes) throws SAXException {
/* 187 */     if ("resource-config".equals(qName)) {
/* 188 */       ResourceConfig resourceConfig = this.m_maker.buildResourceConfig(attributes);

/* 190 */       this.m_entity = resourceConfig;
/* 191 */       this.m_objs.push(resourceConfig);
/* 192 */       this.m_tags.push(qName);
/* 193 */     } else if ("resource".equals(qName)) {
/* 194 */       Resource resource = this.m_maker.buildResource(attributes);

/* 196 */       this.m_entity = resource;
/* 197 */       this.m_objs.push(resource);
/* 198 */       this.m_tags.push(qName);
    } else {
/* 200 */       throw new SAXException("Unknown root element(" + qName + ") found!");
    }
  }

  
  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
/* 206 */     if (uri == null || uri.length() == 0) {
/* 207 */       if (this.m_objs.isEmpty()) {
/* 208 */         parseRoot(qName, attributes);
      } else {
/* 210 */         Object parent = this.m_objs.peek();
/* 211 */         String tag = (String)this.m_tags.peek();

/* 213 */         if (parent instanceof ResourceConfig) {
/* 214 */           parseForResourceConfig((ResourceConfig)parent, tag, qName, attributes);
/* 215 */         } else if (parent instanceof Resource) {
/* 216 */           parseForResource((Resource)parent, tag, qName, attributes);
        } else {
/* 218 */           throw new RuntimeException(String.format("Unknown entity(%s) under %s!", new Object[] { qName, parent.getClass().getName() }));
        }
      }
      
/* 222 */       this.m_text.setLength(0);
    } else {
/* 224 */       throw new SAXException(String.format("Namespace(%s) is not supported by %s.", new Object[] { uri, getClass().getName() }));
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/resource/transform/DefaultSaxParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */